/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var l1 = new ListNode(1, null);
l1.next = new ListNode(2, null);
l1.next.next = new ListNode(4, null);
var l2 = new ListNode(1, null);
l2.next = new ListNode(3, null);
l2.next.next = new ListNode(4, null);
console.log(mergeTwoLists(l1, l2));

function mergeTwoLists(l1, l2) {
    if (l1 == null) { return l2 }
    if (l2 == null) { return l1 }
    while (l1 != null) {
        b = l1.next;
        l2 = insert(l1, l2);
        l1 = b;
    }
    return l2.val + "," + l2.next.val + "," + l2.next.next.val + "," + l2.next.next.next.val + "," + l2.next.next.next.next.val + "," + l2.next.next.next.next.val;
}

function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
}

function insert(a, l2) {
    head = new ListNode(null, l2);
    current = l2;
    pre = head;
    while (a.val > current.val) {
        pre = current;
        current = current.next;
        if (current == undefined) { break; }
    }
    pre.next = a;
    a.next = current;
    return head.next;
}